﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Command.Works;
using MyMES.Read.API.Dtos;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace MyMES.Read.API.Controllers.ZYY.Works
{
    [Route("api/[controller]/[action]")]
    [ApiController]
       public class WorkOrderController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IBaseRepository<WorkOrder> baseRepository;

        /// <summary>
        /// 生产工单
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="baseRepository"></param>
        public WorkOrderController(IMediator mediator, IBaseRepository<WorkOrder> baseRepository)
        {
            this.mediator = mediator;
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 生产工单显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]   
        public async Task<APIResult<List<WorkOrder>>> GetWorkOrder([FromQuery] GetWordCommand request, CancellationToken cancellationToken)
        {
            return await  mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 客户信息导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<IActionResult> ExeclCustomer()
        {
            try
            {
                APIResult<ArrivalnoSupplierDtos> result = new APIResult<ArrivalnoSupplierDtos>();
                result.Code = APIEnum.查询成功;
                result.Msg = "客户信息导出成功";
                var sql = "SELECT *  FROM WorkOrder where IsDlete=false ORDER BY  WorkOrderId DESC;";
                var Rlist = await baseRepository.SelectAsync(sql);

                //创建工作簿
                IWorkbook workbook = new XSSFWorkbook();

                //依据工作簿创建sheet
                ISheet sheet = workbook.CreateSheet("生产工单信息");
                //依据sheet创建首行
                IRow row = sheet.CreateRow(0);
                //在创建好的行中创建首行单元格
                row.CreateCell(0).SetCellValue("工单编码");
                row.CreateCell(1).SetCellValue("工单名称");
                row.CreateCell(2).SetCellValue("工单类型");
                row.CreateCell(3).SetCellValue("工单来源");
                row.CreateCell(4).SetCellValue("订单编号");
                row.CreateCell(5).SetCellValue("产品编号");
                row.CreateCell(6).SetCellValue("产品名称");
                row.CreateCell(7).SetCellValue("规格型号");
                row.CreateCell(8).SetCellValue("单位");
                row.CreateCell(9).SetCellValue("工单数量");
                row.CreateCell(10).SetCellValue("调整数量");


                //行编号
                int index = 1;
                foreach (var item in Rlist.ToList())
                {
                    //sheet表中的第二行完成数据遍历
                    //创建新行
                    row = sheet.CreateRow(index);

                    //将所有数据依次写入到行中
                    row.CreateCell(0).SetCellValue(item.WorkOrderCoding);
                    row.CreateCell(1).SetCellValue(item.WorkOrderName);
                    row.CreateCell(2).SetCellValue(item.WorkOrderType);
                    row.CreateCell(3).SetCellValue(item.WorkorderSource);
                    row.CreateCell(4).SetCellValue(item.OrderNumber);
                    row.CreateCell(5).SetCellValue(item.ProductNumber);
                    row.CreateCell(6).SetCellValue(item.ProductName);
                    row.CreateCell(7).SetCellValue(item.Specifications);
                    row.CreateCell(8).SetCellValue(item.Unit);
                    row.CreateCell(9).SetCellValue(item.WorkOrderNum);
                    row.CreateCell(10).SetCellValue(item.AdjustQuantity);
                    //换行
                    index++;
                }

                //excel
                byte[] buffer;
                //使用内存流出Excel的内容
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    buffer = ms.ToArray();
                }
                //导出Excel  using Microsoft.AspNetCore.StaticFiles;
                string excelMime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];


                //导出
                return File(buffer, excelMime, "生产工单信息.xlsx");
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
}
